//
//  UIView+NLCustomHud.m
//  AncientVillage
//
//  Created by yj_zhang on 2018/11/23.
//  Copyright © 2018年 LSJ. All rights reserved.
//

#import "UIView+NLCustomHud.h"
#import "MantisGCDHandle.h"
#import "CommonHeader.h"


@implementation UIView (NLCustomHud)

-(void)nlShowHudLoadingWithText:(NSString *)text{
    [MantisGCDTool mainBlock:^{
        MBProgressHUD *_hud = [self selfNlHud];
        _hud.mode = MBProgressHUDModeIndeterminate;
        _hud.bezelView.alpha = 0.7f;
        _hud.label.textColor =[UIColor whiteColor];
        if (text && text.length > 0) {
            _hud.label.text = text;
        }
        _hud.detailsLabel.text = @"";
        [_hud showAnimated:YES];
    }];
}

- (void)nlHideHudLoading{
    [MantisGCDTool mainBlock:^{
        MBProgressHUD *_hud = [self selfNlHud];
        [_hud hideAnimated:YES];
    }];
}

- (void)nlShowHudText:(NSString *)text hideDelay:(NSTimeInterval)delay completionBlock:(nullable MBProgressHUDCompletionBlock)completion{
    [MantisGCDTool mainBlock:^{
        MBProgressHUD *_hud = [self selfNlHud];
        _hud.mode = MBProgressHUDModeText;
        _hud.completionBlock = completion;
        _hud.bezelView.alpha = 0.7f;
        _hud.label.textColor = [UIColor whiteColor];

        if (text && text.length > 0) {
            _hud.label.text = text;
        }
        _hud.detailsLabel.text = @"";
        [_hud showAnimated:YES];
//        [_hud hideAnimated:YES afterDelay:delay];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(delay * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [_hud hideAnimated:YES];
        });
    }];
    
}
- (void)nlShowHudLoading{
    MBProgressHUD *_hud = [self selfNlHud];
    _hud.mode = MBProgressHUDModeIndeterminate;
    _hud.label.text = @"";
    _hud.detailsLabel.text = @"";
    _hud.bezelView.alpha = 0.8f;
    [_hud showAnimated:YES];
}
//MARK:Private Methods
- (MBProgressHUD *)selfNlHud{
    MBProgressHUD *_hud = [MBProgressHUD HUDForView:self];
    if (_hud == nil) {
        _hud = [[MBProgressHUD alloc] initWithView:self];
        [UIActivityIndicatorView appearanceWhenContainedInInstancesOfClasses:@[[MBProgressHUD class]]].color = [UIColor whiteColor];
        _hud.label.textColor = [UIColor whiteColor];
        [self addSubview:_hud];
        [self bringSubviewToFront:_hud];
    }
    _hud.label.font = KFontSize(8);
    _hud.bezelView.backgroundColor = [UIColor blackColor];
    _hud.bezelView.alpha = 0.7f;
    _hud.label.textColor = [UIColor whiteColor];
    _hud.margin = 18;
    _hud.bezelView.layer.cornerRadius = 10.f;
    return _hud;
}

@end


